package com.smg.community.controller;

import java.util.HashMap;
import java.util.Map;

import javacommon.base.BaseRestSpringController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jpush.api.ErrorCodeEnum;
import cn.jpush.api.MessageResult;

import com.google.gson.Gson;
import com.smg.community.model.SmgMember;
import com.smg.community.model.TalkHistory;
import com.smg.community.service.SmgTalkHistoryManager;
import com.smg.community.service.TalkService;

@Controller
@RequestMapping("/talk")
public class TalkController extends BaseRestSpringController<SmgMember, Integer>{
	
		
	
	@Resource(name = "talkService")
	private TalkService talkService;
	
	@Autowired
	private SmgTalkHistoryManager smgTalkHistoryManager;

	@RequestMapping(value = "/single")
	public @ResponseBody Map<String,Object> talkToSingle(HttpServletRequest req, HttpServletResponse resp) {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			long start =  System.currentTimeMillis();
			map.put("进入方法 currentTimeMillis",start+"");
			String message = req.getParameter("message");
			Gson gson = new Gson();
			TalkHistory talkHistory = gson.fromJson(message, TalkHistory.class);
		 	smgTalkHistoryManager.save(talkHistory);  
		 	long step1 = System.currentTimeMillis();
		 	map.put("保存数据库 currentTimeMillis",step1+"");
		 	map.put("保存数据库耗费时间",(step1-start)/1000+"");
			// TODO 保存到数据库中
			// TODO 把数据库主键塞回到talkHistory.setServerSideId
			// TODO 再把这个对象解析成json返回给我,并且返回发送成功与否
		 	String uuid = RequestController.memberIdDeviceIdMap.get(talkHistory.getTargetId());
		 	if  ( uuid == null ){
		 		String memberId = talkHistory.getTargetId();
		 		SmgMember member = smgMemberManager.getById(Integer.parseInt(memberId));
		 		uuid = member.getUuid();
		 		RequestController.memberIdDeviceIdMap.put(talkHistory.getTargetId(),uuid);
		 	}
		 	long st2 = System.currentTimeMillis();  
		 	map.put("talkService开始发消息",st2+"");
			MessageResult result = talkService.sendmsg(uuid,
					talkHistory.getTargetId(), "", gson.toJson(talkHistory));
			long st3 = System.currentTimeMillis();
			map.put("talkService发消息结束",st3+"");
			map.put("talkService发消息耗费时间",(st3-st2)/1000+"");
			map.put("uuid",uuid);
			// = talkService.sendmsg(content, alias, title);
			 if (result.getErrcode() == ErrorCodeEnum.NOERROR.value()) {
			 // 返回成功给我
				 map.put("state",1);
				 map.put("talkHistory",talkHistory);
			 } else {
				 map.put("state",-1);
				// map.put("desc",result.getErrcode()+"   "+result.getErrmsg());
				 map.put("desc","对方目前不在线,可能没有办法回复你哦.");

			 }
			 System.out.println("messageResult gson  :"+gson.toJson(result));
		} catch (Exception e) {
			System.out.println(e);
			 map.put("state",-99);
		}
		return map;
	}

}
